package com.craftplay.service;


import com.craftplay.dao.FocusMapper;
import com.craftplay.model.BehaveModel;
import com.craftplay.model.Focus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class FocusService {

    @Autowired
    private FocusMapper focusMapper;

    @Autowired
    private BehaveService behaveService;

    /**
     * 关注用户
     *
     * @param userId
     * @param focusUserId
     * @return
     */
    public int focusUser(int userId, int focusUserId) {
        Focus focus = new Focus();
        //是否已经关注
        boolean focusUser = isFocusUser(userId, focusUserId);
        if (!focusUser) {
            focus.setFocusUserId(focusUserId);
            focus.setUserId(userId);
            focus.setFocusTime(new Date());

            BehaveModel behaveModel = new BehaveModel();
            behaveModel.setUserId(focusUserId);
            behaveModel.setBuserId(userId);
            behaveModel.setBehave("关注");
            behaveModel.setCreateTime(new Date());
            behaveService.insertBehave(behaveModel);

            return focusMapper.insert(focus);
        }
        return 1;
    }

    /**
     * 取消关注
     *
     * @param userId
     * @param focusUserId
     * @return
     */
    public int unFocusUser(int userId, int focusUserId) {
        Focus focus = new Focus();
        focus.setFocusUserId(focusUserId);
        focus.setUserId(userId);
        focus.setFocusTime(new Date());
        return focusMapper.deleteByFocusUserId(focus);
    }


    /**
     * 关注的用户
     *
     * @param userId
     * @return
     */
    public List<Focus> myFocusUser(int userId) {
        return focusMapper.selectByUserId(userId);
    }

    /**
     * 关注的用户数量
     *
     * @param userId
     * @return
     */
    public int myFocusUserNum(int userId) {
        return focusMapper.myFocusUserNum(userId);
    }


    /**
     * 粉丝
     *
     * @param userId
     * @return
     */
    public List<Focus> focusMyUser(int userId) {
        return focusMapper.selectByFocusUserId(userId);
    }


    /**
     * 粉丝数量
     *
     * @param userId
     * @return
     */
    public int focusMyUserNum(int userId) {
        return focusMapper.selectByFocusUserIdNum(userId);
    }


    /**
     * 是否关注
     *
     * @param userId
     * @param focusUserId
     * @return
     */
    public boolean isFocusUser(int userId, int focusUserId) {
        Focus focus = focusMapper.selectByUserIdAndFocusId(userId, focusUserId);
        if (focus == null) {
            return false;
        }
        return true;
    }


}
